<?php

namespace frontend\modules\Rank\controllers;

use common\models\c2\entity\ActivityModel;
use common\models\c2\entity\ActivityPlayerModel;
use common\models\c2\statics\ActivityPlayerState;
use common\models\c2\statics\Whether;
use frontend\controllers\ActivityController;
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;

/**
 * Default controller for the `rank` module
 */
class DefaultController extends ActivityController
{
    /**
     * Renders the index view for the module
     * @param null $c
     * @return string
     * @throws NotFoundHttpException
     */
    public function actionIndex($c = null)
    {
        $activity = ActivityModel::findOne(['seo_code' => $c]);
        if (is_null($activity) || $activity->is_released == Whether::TYPE_NO) {
            throw new NotFoundHttpException(Yii::t('app.c2', 'Activity disable.'));
        }
        $this->activity = $activity;

        $data = ActivityPlayerModel::find()->where(['activity_id' => $this->activity->id])
            ->andFilterWhere(['state' => ActivityPlayerState::STATE_CHECKED])
            ->orderBy(['total_vote_number' => SORT_DESC])->limit(10)->all();
        return $this->render('index', [
            'data' => $data
        ]);
    }
}
